package com.example.internalstorage;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.internalstorage.databinding.ActivityMainBinding;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initView();
        checkRemember();
    }

    private void initView() {
        binding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = binding.etUsername.getText().toString();
                String password = binding.etPassword.getText().toString();

                if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
                    if(binding.cbRemember.isChecked()) {
                        remember(username, password);
                    } else {
                        clear();
                    }
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void remember(String username, String password) {
        try(FileOutputStream fos = openFileOutput("user.txt", Context.MODE_PRIVATE)) {
            fos.write((username + "," + password).getBytes());

            // 或者使用BufferedWriter直接写字符串
            // BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos));
            // writer.write(username + ", " + password);
            // writer.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void checkRemember() {
        try(FileInputStream fis = openFileInput("user.txt")) {
            if(fis.available() == 0) {
                Toast.makeText(this, "文件内容为空", Toast.LENGTH_SHORT).show();
                return;
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
            String[] datas = reader.readLine().split(",");
            if(datas.length > 1) {
                binding.etUsername.setText(datas[0]);
                binding.etPassword.setText(datas[1]);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void clear() {
        try {
            // 清除文件内容
            FileOutputStream fos = openFileOutput("user.txt", Context.MODE_PRIVATE);
            fos.write("".getBytes());
            fos.close();

            // 直接删除文件
            deleteFile("user.txt");
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}